package dk.jagtogjaeger;

import java.util.Collection;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableRow;
import android.widget.TextView;
import dk.jagtogjaeger.domain.Controller;
import dk.jagtogjaeger.domain.HuntJournal;

public class JournalActivity extends Activity {
	/** Called when the activity is first created. */

	private Controller controller = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Hides the title bar.
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.main);

		ImageButton b = (ImageButton) findViewById(R.id.huntJournalButton);
		b.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.nybgtoogleimage));

		controller = new Controller(getBaseContext());
		setUpJournalView();

		Intent intent = getIntent();
		if (intent.hasExtra("newJournalCreated")) {
			// Toast.makeText(getBaseContext(), R.string.journalAdded,
			// Toast.LENGTH_LONG);
		}
		if (intent.hasExtra("journalDeleted")) {
			// Toast.makeText(getBaseContext(), R.string.journalAdded,
			// Toast.LENGTH_LONG);
		}
		

	}
	public void menuButtonsClickHandler(View view) {
		// Calls A Static method, instead of duplication this code 4 times
		// through the activities.
		finish();
		ButtonMenu.ButtenMenuClickHandler(view, this);
	}

	private void setUpJournalView() {
		LinearLayout acll = (LinearLayout) findViewById(R.id.activityContentLinearLayout);
		ScrollView contentforjournals = new ScrollView(this);
		LinearLayout toacll = new LinearLayout(this);
		toacll.setOrientation(LinearLayout.VERTICAL);
		// adds the linear layout to the scrollview
		contentforjournals.addView(toacll);
		acll.addView(contentforjournals);

		// Next lines makes a row with a button.
		TableRow buttonRow = new TableRow(this);
		buttonRow.setPadding(10, 10, 10, 10);
		Button createJournalButton = new Button(this);
		createJournalButton.setText(R.string.creatJournal);
		createJournalButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.imgadd_large));
		createJournalButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(),
						CRUDJournalActivity.class);
				startActivity(intent);
			}
		});

		buttonRow.addView(createJournalButton);
		toacll.addView(buttonRow);
	
		Collection<HuntJournal> journals = controller.getJournals();
		if (journals == null) {
			TextView text = new TextView(this);
			text.setText("No Journals Found.");
			acll.addView(text);
		} else {
			for (final HuntJournal journal : journals) {
				Button journalsbutton = new Button(this);
				journalsbutton.setBackgroundDrawable(getResources()
						.getDrawable(R.drawable.journalrow));
				journalsbutton.setGravity(Gravity.CENTER);
				journalsbutton.setTextColor(Color.WHITE);
				journalsbutton.setText(journal.toString());
				journalsbutton.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(v.getContext(),
								CRUDJournalActivity.class);

						intent.putExtra("journal", journal);
						startActivity(intent);
					}
				});

				toacll.addView(journalsbutton);
			}
		}

	}

}